{%- macro field_label(field) -%}
    <label for="{{ field.id }}">{{ field.label.text }}</label>
{% endmacro %}


{%- macro field_description(field) -%}
    {% if field.description %}
        <span class="help-block">{{ field.description|safe }}</span>
    {% endif %}
{%- endmacro -%}


{%- macro field_errors(field) -%}
    {% if field.errors %}
        {%- for error in field.errors -%}
        <span class="help-block">{{error}}</span>
        {%- endfor -%}
    {% endif %}
{%- endmacro -%}


{%- macro render_quickreply(field, rows, cols, div_class='') -%}
{%- if kwargs['required'] or field.flags.required -%}
    {% if div_class %}
        {{ field(class=div_class, required="required", cols=cols, rows=rows, placeholder=field.label.text, **kwargs) }}
    {% else %}
        {{ field(class="new-message", required="required", cols=cols, rows=row, placeholder=field.label.text, **kwargs) }}
    {% endif %}
{%- else -%}
    {% if div_class %}
        {{ field(class=div_class, cols=cols, rows=row, placeholder=field.label.text, **kwargs) }}
    {% else %}
        {{ field(class="new-message", cols=cols, rows=row, placeholder=field.label.text, **kwargs) }}
    {% endif %}
{%- endif -%}

{{ field_description(field) }}
{{ field_errors(field) }}
{%- endmacro -%}


{# Renders a non bootstrap input field #}
{%- macro render_input_field(field, div_class='', placeholder='') -%}
{%- if div_class -%}
    <div class="{{ div_class }}">
{%- endif -%}

{%- if placeholder -%}
    {% set field_placeholder = placeholder %}
{%- else -%}
    {% set field_placeholder = field.label.text %}
{%- endif -%}

{%- if kwargs['required'] or field.flags.required -%}
    {{ field(required="required", placeholder=field_placeholder) }}
{%- else -%}
    {{ field(placeholder=field_placeholder) }}
{% endif %}

{%- if div_class -%}
    </div>
{%- endif -%}

{{ field_description(field) }}
{{ field_errors(field) }}
{%- endmacro -%}


{%- macro render_boolean_field(field, inline=False) -%}
<div class="checkbox{% if inline %}inline{% endif %}{% if field.errors %}has-error{% endif %}">
    <label>
    {{ field(**kwargs) }}
    {{ field.label.text }}
    </label>
    {{ field_description(field) }}
    {{ field_errors(field) }}
</div>
{%- endmacro -%}


{%- macro render_select_field(field, div_class='', select_class="form-control") -%}
<div class="form-group">
    {% if div_class %}
    <div class="{{ div_class }}">
    {% else %}
    <div class="col-sm-5">
    {% endif %}
        <label>{{ field.label.text }}</label>
        {% if field.type == 'QuerySelectMultipleField' or field.type == 'SelectMultipleField' %}
            {{ field(multiple=True, class=select_class) }}
        {% else %}
            {{ field(class=select_class) }}
        {%- endif -%}

        {{ field_description(field) }}
        {{ field_errors(field) }}
    </div>
</div>
{%- endmacro -%}


{%- macro render_submit_field(field, div_class='', input_class='') -%}
{% if div_class %}
<div class="{{ div_class }}">
{% endif %}

    {{ field(class=input_class or 'btn btn-success') }}

{% if div_class %}
</div>
{% endif %}
{%- endmacro -%}


{%- macro render_field(field, with_label=True, div_class='', rows='') -%}
<div class="form-group{%- if field.errors %} has-error{%- endif %}">

    <div class="{%- if div_class -%}{{ div_class }}{%- else -%}col-sm-5{%- endif -%}">
        {% if with_label %}
            <label>{{ field.label.text }}</label>
        {% endif %}

        {%- if kwargs['required'] or field.flags.required -%}
            {% if rows %}
                {{ field(class="form-control", required="required", rows=rows, placeholder=field.label.text, **kwargs) }}
            {% else %}
                {{ field(class="form-control", required="required", placeholder=field.label.text, **kwargs) }}
            {% endif %}
        {%- else -%}
            {% if rows %}
                {{ field(class="form-control", rows=rows, placeholder=field.label.text, **kwargs) }}
            {% else %}
                {{ field(class="form-control", placeholder=field.label.text, **kwargs) }}
            {% endif %}
        {%- endif -%}

        {{ field_description(field) }}
        {{ field_errors(field) }}
    </div>
</div>
{%- endmacro -%}


{%- macro inline_field(field, label_text='', label_class='') -%}
<div class="form-group {%- if field.errors %} has-error{%- endif %}">
    {{field.label(class="sr-only")}}

    <div class="col-sm-4">
    {%- if kwargs['required'] or field.flags.required -%}
        {% if label_text %}
            {{field(class='form-control', placeholder=label_text, required="required", **kwargs)}}
        {% else %}
            {{field(class='form-control', placeholder=field.label.text, required="required", **kwargs)}}
        {% endif %}
    {%- else -%}
        {% if label_text %}
            {{field(class='form-control', placeholder=label_text, **kwargs)}}
        {% else %}
            {{field(class='form-control', placeholder=field.label.text, **kwargs)}}
        {% endif %}
    {%- endif -%}
    {{ field_description(field) }}
    {{ field_errors(field) }}
    </div>
</div>
{%- endmacro -%}


{%- macro group_field(field, label_text='', label_class='', css_class='form-control form-grouped') -%}
    <div class="form-group {%- if field.errors %} has-error{%- endif %}" style="margin-bottom: 0px;">
        {{field.label(class="sr-only")}}

        {%- if kwargs['required'] or field.flags.required -%}
            {% if label_text %}
                {{field(class=css_class, placeholder=label_text, required="required", **kwargs)}}
            {% else %}
                {{field(class=css_class, placeholder=field.label.text, required="required", **kwargs)}}
            {% endif %}
        {%- else -%}
            {% if label_text %}
                {{field(class=css_class, placeholder=label_text, **kwargs)}}
            {% else %}
                {{field(class=css_class, placeholder=field.label.text, **kwargs)}}
            {% endif %}
        {%- endif -%}
        {{ field_description(field) }}
        {{ field_errors(field) }}
    </div>
{%- endmacro -%}


{%- macro input_group_field(field, label_text='', css_class='form-control') -%}
    {%- if kwargs['required'] or field.flags.required -%}
        {% if label_text %}
            {{field(class=css_class, placeholder=label_text, required="required", **kwargs)}}
        {% else %}
            {{field(class=css_class, placeholder=field.label.text, required="required", **kwargs)}}
        {% endif %}
    {%- else -%}
        {% if label_text %}
            {{field(class=css_class, placeholder=label_text, **kwargs)}}
        {% else %}
            {{field(class=css_class, placeholder=field.label.text, **kwargs)}}
        {% endif %}
    {%- endif -%}
{%- endmacro -%}


{%- macro horizontal_select_field(field, div_class='', label_class='', select_class="form-control", surrounded_div="col-sm-4") -%}
<div class="form-group row {%- if field.errors %} has-error{%- endif %}">
    {% if label_class %}
        {{ field.label(class=label_class) }}
    {% else %}
        {{ field.label(class="col-sm-3 control-label") }}
    {% endif %}

    {% if div_class %}
        <div class="{{ div_class }}">
    {% else %}
        <div class="col-sm-5">
    {% endif %}

        <div class="row">
        {% if field.type == 'QuerySelectMultipleField' or field.type == 'SelectMultipleField' %}
            {{ field(multiple=True, class=select_class, surrounded_div=surrounded_div) }}
        {% else %}
            {{ field(class=select_class, surrounded_div=surrounded_div) }}
        {%- endif -%}
        </div>
        {{ field_description(field) }}
        {{ field_errors(field) }}
    </div>
</div>
{%- endmacro -%}


{%- macro horizontal_boolean_field(field, div_class='') -%}
<div class="{%- if div_class -%}{{ div_class }}{%- else -%}col-sm-offset-3 col-sm-3{%- endif -%}">
    {{ render_boolean_field(field, **kwargs) }}
</div>
{%- endmacro -%}


{%- macro horizontal_submit_field(field, div_class='', input_class='') -%}
<div class="{%- if div_class -%}{{ div_class }}{%- else -%}col-sm-offset-3 col-sm-3{%- endif -%}">
    {{ field(class=input_class or 'btn btn-success') }}
</div>
{%- endmacro -%}


{%- macro horizontal_field(field, label_text='', label_class='', div_class='', input_class='') -%}
<div class="form-group row {%- if field.errors %} has-error{%- endif %}">

    {% if field.type == "BooleanField" or field.type == "SubmitField" %}
        {% if field.type == "BooleanField" %}
            {{ horizontal_boolean_field(field, div_class) }}
        {% else %}
            {{ horizontal_submit_field(field, div_class) }}
        {% endif %}
    {% else %}

        {% if label_class %}
            {{ field.label(class=label_class) }}
        {% else %}
            {{ field.label(class="col-sm-3 control-label") }}
        {% endif %}

        {% if div_class %}
            <div class="{{ div_class }}">
        {% else %}
            <div class="col-sm-4">
        {% endif %}

        {%- if kwargs['required'] or field.flags.required -%}
            {% if label_text %}
                {{ field(class='form-control', placeholder=label_text, required="required", **kwargs) }}
            {% else %}
                {{ field(class='form-control', placeholder=field.label.text, required="required", **kwargs) }}
            {% endif %}
        {%- else -%}
            {% if label_text %}
                {{ field(class='form-control', placeholder=label_text, **kwargs) }}
            {% else %}
                {{ field(class='form-control', placeholder=field.label.text, **kwargs) }}
            {% endif %}
        {%- endif -%}
    </div> <!-- end div_class -->
    {% endif %}

    {{ field_description(field) }}
    {{ field_errors(field) }}

</div> <!-- end form-group -->
{%- endmacro -%}


{% macro topnav(endpoint, name, icon='', id='', active=False) %}
<li {% if id %}id={{id}}{% endif %} {% if endpoint == request.endpoint or active == True %}class="active"{% endif %}>
    <a href={{ url_for(endpoint) }}>
        {% if icon %}<i class="{{ icon }}"></i> {% endif %}{{ name }}
    </a>
</li>
{% endmacro %}

{% macro is_active(endpoint, active=False) %}
    {%- if endpoint == request.endpoint or active == True -%}
        active
    {%- endif -%}
{% endmacro %}

{% macro navlink(endpoint, name, icon='', active=False) %}
<li {% if endpoint == request.endpoint or active %}class="active"{% endif %}>
    <a href="{{ url_for(endpoint) }}">{% if icon %}<i class="{{ icon }}"></i> {% endif %} {{ name }}</a>
</li>
{% endmacro %}

{% macro tablink_href(endpoint, name, active=False) %}
<li {% if endpoint == request.endpoint or active %}class="active"{% endif %} >
    <a href={{ endpoint }} role="tab" data-toggle="tab">{{ name }}</a>
</li>
{% endmacro %}

{% macro render_pagination(page_obj, url, ul_class='', sort_by=None, asc=True) %}
<ul class='{%- if ul_class -%}{{ ul_class }}{%- else -%}pagination{%- endif -%}'>
    {% set ordering = 'asc' if asc == True else 'desc' %}
    {% set sorting = '&sort_by='+(sort_by|urlencode)+'&order_by='+ordering if sort_by is string else '' %}
    <li class="disabled"><a href="#"><span class="pages-label">{% trans %}Pages{% endtrans %}:</span></a></li>
    {%- for page in page_obj.iter_pages() %}
        {% if page %}
            {% if page != page_obj.page %}
                <li><a href="{{ url }}?page={{ page }}{{ sorting }}">{{ page }}</a></li>
            {% else %}
                <li class="active"><a href="#">{{ page }}</a></li>
            {% endif %}
        {% endif %}
    {%- else -%}
        <li class="active"><a href="#">1</a></li>
    {%- endfor %}
    {% if page_obj.has_next %}
        <li><a href="{{ url }}?page={{ page_obj.next_num }}{{ sorting }}">&raquo;</a></li>
    {% endif %}
</ul>
{% endmacro %}

{% macro render_topic_pagination(page_obj, url) %}
<ul class="pagination pagelink pull-left">
    <li class="disabled"><a><span class="pages-label">Pages: </span></a></li>
    {%- for page in page_obj.iter_pages() %}
        {% if page %}
            {% if page != page_obj.page %}
                <li><a href="{{ url }}?page={{ page }}">{{ page }}</a></li>
            {% else %}
                <li class="disabled"><a href="#">{{ page }}</a></li>
            {% endif %}
        {% endif %}
    {%- else -%}
        <li class="disabled"><a href="#">1</a></li>
    {%- endfor %}
    {% if page_obj.has_next %}
        <li><a href="{{ url }}?page={{ page_obj.next_num }}">Next</a></li>
    {% endif %}
</ul>
{% endmacro %}


{% macro message_pagination(page_obj, url) %}
<ul class='{%- if ul_class -%}{{ ul_class }}{%- else -%}pagination pagination-sm{%- endif -%}'>
    {%- for page in page_obj.iter_pages() %}
        {% if page %}
            {% if page != page_obj.page %}
                <li><a href="{{ url }}?page={{ page }}">{{ page }}</a></li>
            {% else %}
                <li class="active"><a href="#">{{ page }}</a></li>
            {% endif %}
        {% endif %}
    {%- else -%}
        <li class="active"><a href="#">1</a></li>
    {%- endfor %}
    {% if page_obj.has_next %}
        <li><a href="{{ url }}?page={{ page_obj.next_num }}">&raquo;</a></li>
    {% endif %}
</ul>
{% endmacro %}


{# Generates a some kind of pagination for the posts in topic in the forum view. #}
{%- macro topic_pages(topic_obj, per_page=10) -%}
{% set pages = (topic_obj.post_count / per_page)|round(method='ceil')|int %}
{% set max_pages = 4 %}
{% set page_count = pages if pages <= max_pages else max_pages %}

{%- if pages > 1 -%}
[
    {%- for page in range(1, page_count+1) -%}
        {% if pages > max_pages and page == max_pages-1 %}
            <a href="{{ url_for('forum.view_topic', topic_id=topic_obj.id) }}?page={{ page }}">{{ page }}</a> ...
        {% elif pages > max_pages and loop.last %}
            <a href="{{ url_for('forum.view_topic', topic_id=topic_obj.id) }}?page={{ pages }}">{{ pages }}</a>
        {% else %}
            <a href="{{ url_for('forum.view_topic', topic_id=topic_obj.id) }}?page={{ page }}">{{ page }}</a>
        {% endif %}
    {%- endfor -%}
]
{%- endif -%}
{%- endmacro -%}


{# Generates a topic url with an anchor to the post #}
{%- macro generate_post_url(topic, post, page) -%}
    {%- if page > 1 -%}
        {{ topic.url }}?page={{ page }}#pid{{ post.id }}
    {%- else -%}
        {{ topic.url }}#pid{{ post.id }}
    {%- endif -%}
{%- endmacro -%}


{# Generates the post id for the topic. Each topic starts with the post id 1 #}
{%- macro generate_post_id(posts, page, per_page) -%}
    {%- if posts.page == 1 -%}
        {{ page }}
    {%- else -%}
        {{ page + (posts.page - 1) * per_page }}
    {%- endif -%}
{%- endmacro -%}
